package com.zzy.algorithm.linkedList;

import com.zzy.algorithm.model.ListNode;

/**
 * LeetCode -83 :删除排序链表中的重复元素
 *
 * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 *
 * 解题思路：
 * 方法一：快慢指针
 *  当快慢指针相同时，慢指针指向快指针下个节点。不同时慢指针也往前移动一次
 *
 */
public class Q8_DeleteDuplicateFromSortedLinkedList {

    public static void main(String[] args) {
        Q8_DeleteDuplicateFromSortedLinkedList q8_deleteDuplicateFromSortedLinkedList = new Q8_DeleteDuplicateFromSortedLinkedList();
        ListNode solution = q8_deleteDuplicateFromSortedLinkedList.solution(ListNode.list3);
        while (solution != null){
            System.out.println(solution.val);
            solution = solution.next;
        }
    }

    private ListNode solution(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null){
            if(slow.val == fast.val){
                slow.next = fast.next;
            } else {
                slow = slow.next;
            }
            fast = fast.next;
        }
        return head;
    }
}
